<?php


namespace app\admin\controller;

use app\admin\model\Links as LinksModel;

// 运营模块
class Operate extends Base
{
    /**
     * 友情链接
     * date 2020/8/15 14:09
     */
    public function links() {
        if (IS_POST) {
            $search = request()->param();
            $where = [];
            $where['is_del'] = 0;
            $db = LinksModel::where($where)->order('id desc');
            $list = $db->page($search['page'],$search['limit'])->select();
            foreach ($list as &$item) {
                $item['img'] = table_img($item['img']);
                $item['status_switch'] = table_switch('Operate/links_switch', ['id' => $item['id']], $item['status'] == 1);
                $item['time'] = date('Y-m-d H:i:s', $item['time']);
                $item['up_time'] = date('Y-m-d H:i:s', $item['up_time']);
            }
            admin_response(0, 'success', $list, $db->count());
        }

        return $this->render_table([
            "page" => true,
            "toolbar" => [
                table_button('admin/Operate/add_links',"添加","frame", '', ['850px', '600px']),
            ],
            "toolbar_row" => [
                table_button('admin/Operate/update_links',"编辑","frame", '', ['850px', '600px']),
                table_button('admin/Operate/del_links',"删除","confirm"),
            ],
            "cols" => [
                ["type" => "checkbox"],
                ["field" => "title", "title" => "标题", "align" => "center"],
                ["field" => "url", "title" => "URL", "align" => "center"],
                ["field" => "img", "title" => "banner图", "align" => "center"],
                ["field" => "sort", "title" => "排序", "align" => "center"],
                ["field" => "status_switch", "title" => "状态", "align" => "center"],
                ["field" => "time", "title" => "添加时间", "align" => "center"],
                ["field" => "up_time", "title" => "更新时间", "align" => "center"],
            ],

        ], false);
    }

    /**
     * 显示隐藏-友情链接
     * @date 2021/4/2 10:58
     */
    public function links_switch()
    {
        $id = param_check('id');
        $checked = param_check('checked', 0);
        $res = LinksModel::where('id', $id)->update([
            'status' => $checked ? 1 : 0
        ]);
        $res ? json_response(1, '修改成功') : json_response(0, '修改失败');
    }

    /**
     *添加-友情链接
     * @date 2020/8/6 22:31
     */
    public function add_links(){
        if (IS_POST){
            $data = input();
            $data['time'] = time();
            $data['up_time'] = time();
            $res = LinksModel::insertGetId($data);
            $res ? json_response(1,'添加成功！') : son_response(0,'添加失败！');
        }
        return $this->render_form([
            ['type'=>'text', 'name'=>'title', 'title'=>'标题', 'lay-verify'=>'required'],
            ['type'=>'text', 'name'=>'url', 'title'=>'URL', 'lay-verify'=>'required'],
            ['type'=>'upload', 'name'=>'img', 'title'=>'banner图'],
            ["type" => "text", "name" => "sort", "title" => "排序", "lay-verify" => ""],
            ['type'=>'radio', 'name'=>'status', 'title'=>'状态','option'=>[
                ['title'=>'禁用','value'=>0],
                ['title'=>'正常','value'=>1]
            ]],
        ]);

    }

    /**
     *编辑-友情链接
     * @date 2020/8/6 22:31
     */
    public function update_links(){
        $id = \param_check('id');
        if (IS_POST){
            $data = input();
            $data['up_time'] = time();
            $res = LinksModel::where('id',$id)->update($data);
            $res ? json_response(1,'编辑成功！') : son_response(0,'编辑失败！');
        }
        $data = LinksModel::where('id',$id)->find();
        return $this->render_form([
            ['type'=>'text', 'name'=>'title', 'title'=>'标题', 'lay-verify'=>'required'],
            ['type'=>'text', 'name'=>'url', 'title'=>'URL', 'lay-verify'=>'required'],
            ['type'=>'upload', 'name'=>'img', 'title'=>'banner图'],
            ["type" => "text", "name" => "sort", "title" => "排序", "lay-verify" => ""],
            ['type'=>'radio', 'name'=>'status', 'title'=>'状态','option'=>[
                ['title'=>'禁用','value'=>0],
                ['title'=>'正常','value'=>1]
            ]],
        ],$data);

    }

    /**
     *删除-友情链接
     * @date 2020/8/6 22:31
     */
    public function del_links(){
        $id = \param_check('id');
        LinksModel::where('id',$id)->update([
            'is_del'        => 1
        ])  ? json_response(1,'删除成功！') : json_response(0,'删除失败！');
    }

}